﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
    public static partial class contest
    {
        public static dynamic submitCode(int cid, char ps, int lang, string code, string username,bool god)
        {
            var sql = new Sql.SqlDataContext();
            if (!sql.ContestProblem.Any(row => row.ps == ps && row.cid == cid)) return new
                  {
                      success = false,
                      msg = "题目不存在"
                  };
            else if (!sql.Language.Any(row => row.code == lang)) return new
                 {
                     success = false,
                     msg = "语言不存在"
                 };
            else if(!sql.Contest.Any(row => row.cid == cid)) return new{
                success = false,
                msg = "比赛不存在"
            };
            else if(sql.Contest.Where(row => cid == row.cid).FirstOrDefault().overTime < DateTime.Now) return new{
                success = false,
                msg = "比赛已经结束"
            };
            var s = new Sql.ContestStatus
            {
                cid = cid,
                ps = ps,
                lang = lang,
                code = code,
                username = username,
                status = 0,
                submitTime = DateTime.Now,
                god = god
            };
            sql.ContestStatus.InsertOnSubmit(s);
            sql.SubmitChanges();
            return new
            {
                success = true
            };
        }
    }
}
